package com.ceair.mub2c.payment.payments.utils;

import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

import com.ceair.mub2c.common.utils.PropertiesUtil;

import sun.misc.BASE64Decoder;

public class WalletUtils {

	/** 指定加密算法为RSA */
	private static final String ALGORITHM = "RSA";

	/**
	 * AES解密
	 * 
	 * @param respBody
	 * @param aeskey2
	 * @return
	 */
	public static String getAESDeciphering(String respBody, String aeskey2) {
		String result = AES.decrypt(respBody, aeskey2);
		return result;
	}

	/**
	 * MD5验签
	 * 
	 * @param respBody
	 * @param respSign
	 * @param md5key2
	 * @return
	 * @return
	 */
	public static Boolean checkResp(String respBody, String respSign, String md5key2) {

		Boolean result = false;
		MD5Hash md5hash = new MD5Hash();

		String respBodyCheck = md5hash.getMD5ofStrByCharset(respBody + "&" + md5key2, "UTF-8");

		if (respBodyCheck.equals(respSign)) {
			result = false;
		} else {
			result = true;
		}

		return result;
	}

	/**
	 * Md5加密
	 * 
	 * @param body
	 * @param md5key2
	 * @return
	 */
	public static String getMd5Sign(String body, String md5key2) {
		MD5Hash md5hash = new MD5Hash();
		String result = md5hash.getMD5ofStrByCharset(body + "&" + md5key2, "UTF-8");
		return result;
	}

	/**
	 * Aes加密
	 * 
	 * @param jsonString
	 * @param aeskey2
	 * @return
	 */
	public static String getAesmisencrypt(String jsonString, String aeskey2) {
		String result = AES.encrypt(jsonString, aeskey2);
		return result;
	}
	
	  /** 
     * 加密方法 RSA
     * @param source 源数据 
     * @return 
     * @throws Exception 
     */  
    public static String encryptRSA(String source,String publicKeyStr) throws Exception {
    	KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        BASE64Decoder decoder = new BASE64Decoder();
    	byte[] publicKeyByte = decoder.decodeBuffer(publicKeyStr);  
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyByte);
        Key publicKey = keyFactory.generatePublic(x509KeySpec);
        /** 得到Cipher对象来实现对源数据的RSA加密 */  
        Cipher cipher = Cipher.getInstance(ALGORITHM);  
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
        byte[] b = source.getBytes();  
        /** 执行加密操作 */  
        byte[] b1 = cipher.doFinal(b);  
        return new Base64().encodeToString(b1);
    }  
  
    /** 
     * 解密算法  RSA
     * @param cryptograph    密文 
     * @return 
     * @throws Exception 
     */  
    public static String decryptRSA(String cryptograph,String privateKeyStr) throws Exception {
    	KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
    	Base64 base64 = new Base64();
    	byte[] privateKeyByte = base64.decode(privateKeyStr);  
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyByte);
        Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
        /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */  
        Cipher cipher = Cipher.getInstance(ALGORITHM);  
        cipher.init(Cipher.DECRYPT_MODE, privateKey);  
        byte[] b1 = base64.decode(cryptograph);  
        /** 执行解密操作 */  
        byte[] b = cipher.doFinal(b1);  
        return new String(b);  
    }  
}
